<?php
include('km_smtp_class.php');

function check($host, $find) {
    $fp = fsockopen($host, 80, $errno, $errstr, 10);
    if (!$fp) {
        echo "$errstr ($errno)\n";
    } else {
       $header = "GET / HTTP/1.1\r\n";
       $header .= "Host: $host\r\n";
       $header .= "Connection: close\r\n\r\n";
       fputs($fp, $header);
       while (!feof($fp)) {
           $str .= fgets($fp, 1024);
       }
       fclose($fp);
       return (strpos($str, $find) !== false);
    }
}

function alert($host) 
	{
	$mail = new KM_Mailer("smtp.gmail.com", "587", "incrdbleroot@gmail.com", "tTypS13D", "tls");
	if($mail->isLogin) {
  		/* $mail->send(from, to, subject, body, headers = optional) */
		$mail->send('incrdbleroot@gmail.com', 'thiagoalmeidasa@gmail.com', 'Monitoring', 'Alert  $host down!');
		$mail->clearRecipients();
		$mail->clearCC();
		$mail->clearBCC();
		$mail->clearAttachments();
	} else {
		echo "Login failed <br>";
		}
	}

$host = $_POST["furl"];
$find = $_POST["find"];
if (!check($host, $find)) echo "alert" && alert($host);
else echo "ok"
?>
